package com.ukao.steward.ui.valetRunners.bingCard;


import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.base.BaseFragment;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.bean.UserInfoCard;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.RegularUtils;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.ClearEditText;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 搜索用戶
 */
public class SearchUserFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.cardName)
    TextView cardName;
    @BindView(R.id.cardDiscountName)
    TextView cardDiscountName;
    @BindView(R.id.attr)
    TextView attr;
    @BindView(R.id.cardNo)
    TextView cardNo;
    @BindView(R.id.balance)
    TextView balance;
    @BindView(R.id.phone)
    ClearEditText phone;
    @BindView(R.id.confirm_btn)
    Button searchBtn;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private UserInfoCard muserInfoCard;
    private boolean isStoreCreatOrderEnter;


    public static SearchUserFragment newInstance(Parcelable muserInfoCard, boolean isStoreCreatOrderEnter) {
        SearchUserFragment fragment = new SearchUserFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, muserInfoCard);
        args.putBoolean(ARG_PARAM2, isStoreCreatOrderEnter);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            muserInfoCard = getArguments().getParcelable(ARG_PARAM1);
            isStoreCreatOrderEnter = getArguments().getBoolean(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_search_user, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        fillData();
    }


    @Override
    protected void initView() {
        title.setText("搜索客户");
        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
    }

    private void fillData() {
        if (!CheckUtils.isNull(muserInfoCard)) {
            cardName.setText(CheckUtils.isEmptyString(muserInfoCard.getCardName()));
            cardNo.setText(RegularUtils.formatNum(muserInfoCard.getCardNo()));
            attr.setText(CheckUtils.isEmpty(muserInfoCard.getAttr())?"":"所属："+muserInfoCard.getAttr());
            cardDiscountName.setText(CheckUtils.isEmptyString(muserInfoCard.getCardDiscountName()));
            balance.setText("￥"+CheckUtils.isEmptyNumber(muserInfoCard.getBalance()));


        }
    }


    /**
     * 手机号搜索
     */
    public void queryKeyword(final  String keyword,String  rfidId) {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("phone",keyword);
        mHashMap.put("rfidId",rfidId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores().queryKeyword(Constant.createParameter(mHashMap)),
                new ApiCallback<SearchUserBean>() {
                    @Override
                    public void onSuccess(SearchUserBean model) {

                        if (model.getHttpCode() == 200) {
                            SearchUserBean mSearchUserBean=model.getData();
                            if (mSearchUserBean.getIsRegister()!=0){/*注册过得用户*/
                                start(ClientFragment.newInstance(muserInfoCard,mSearchUserBean,isStoreCreatOrderEnter));
                            }else{/*未注册*/
                                start(BuildUserFragment.newInstance(keyword,isStoreCreatOrderEnter));
                            }

                        } else {


                            T.show(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        dismissProgressDialog();

                    }
                });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_btn,R.id.confirm_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.confirm_btn:
                String   strphone=getText(phone);
                if (!RegularUtils.isMobileSimple(strphone)) {
                    T.show("请输入正确手机号");
                    return;
                }
                if (!CheckUtils.isNull(muserInfoCard))
                queryKeyword(strphone,muserInfoCard.getRefidId()+"");

                break;



        }
    }
}
